package top.fzqblog.firstapp;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscriber;
import top.fzqblog.adapter.XRVAdapter;
import top.fzqblog.model.Meizi;
import top.fzqblog.model.Results;
import top.fzqblog.service.RequestService;

/**
 * Created by 抽离 on 2017/9/27 0027.
 */

public class XRecyclerViewTest extends AppCompatActivity {

    @BindView(R.id.XRecyclerView)
    public XRecyclerView xRecyclerView;

    private XRVAdapter xrvAdapter;

    private int page = 1;

    private int total = 200;

    private ArrayList<String> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xrecyclerview_layout);
        ButterKnife.bind(this);
        xRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallPulse);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        RequestService requestService = new RequestService();
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                list = new ArrayList<>();
                requestService.getMeizi(new Subscriber<Meizi>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable throwable) {

                    }

                    @Override
                    public void onNext(Meizi meizi) {
                        xrvAdapter = new XRVAdapter(XRecyclerViewTest.this, meizi.getResults());
                        xrvAdapter.notifyDataSetChanged();
                        xRecyclerView.setAdapter(xrvAdapter);
                        xRecyclerView.refreshComplete();
                        //设置点击查看大图
                        for (Results results : meizi.getResults()) {
                            list.add(results.getUrl());
                        }
                        xrvAdapter.setOnclickItem(position -> {
                            Intent intent = new Intent(XRecyclerViewTest.this, PhotoSee.class);
                            intent.putStringArrayListExtra("imgs", list);
                            intent.putExtra("position", position);
                            startActivity(intent);
                        });
                        page ++;
                    }
                }, "福利", 20, page);

            }

            @Override
            public void onLoadMore() {
                requestService.getMeizi(new Subscriber<Meizi>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable throwable) {

                    }

                    @Override
                    public void onNext(Meizi meizi) {
                        if(page * 20 >= total){
                            xRecyclerView.setNoMore(true);
                            return;
                        }
                        xrvAdapter.addAll(meizi.getResults());
                        xRecyclerView.loadMoreComplete();//加载完成
                        xrvAdapter.notifyDataSetChanged();
                        for (Results results : meizi.getResults()) {
                            list.add(results.getUrl());
                        }
                        //设置点击查看大图
                        xrvAdapter.setOnclickItem(position -> {
                            Intent intent = new Intent(XRecyclerViewTest.this, PhotoSee.class);
                            intent.putStringArrayListExtra("imgs", list);
                            intent.putExtra("position", position);
                            startActivity(intent);
                        });
                        page++;
                    }
                }, "福利", 20, page);
            }
        });

        xRecyclerView.refresh();
    }


}
